import Vue from 'vue'
import Router from 'vue-router'
import Index from './views/Index.vue'
import Register from './views/Register'
import Nofind from './views/404'
import Login from './views/Login'
import Home from './views/Home'
import InfoShow from './views/InfoShow'
import FoundList from './views/FoundList'

Vue.use(Router)

const router = new Router({
    base: process.env.BASE_URL,
    routes: [{
            path: '*',
            name: '/404',
            component: Nofind
        }, {
            path: '/',
            redirect: '/index'
        }, {
            path: '/register',
            name: 'register',
            component: Register
        }, {
            path: '/login',
            name: 'login',
            component: Login
        }, {
            path: '/index',
            name: 'index',
            component: Index,
            children: [{
                path: '',
                component: Home
            }, {
                path: '/home',
                name: 'home',
                component: Home
            }, {
                path: '/infoshow',
                name: 'infoshow',
                component: InfoShow
            }, {
                path: '/foundlist',
                name: 'foundlist',
                component: FoundList
            }]
        },

        // {
        //   path: '/about',
        //   name: 'about',
        //   // route level code-splitting
        //   // this generates a separate chunk (about.[hash].js) for this route
        //   // which is lazy-loaded when the route is visited.
        //   component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
        // }
    ]
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
    const isLogin = localStorage.eleToken ? true : false;
    if (to.path == "/login" || to.path == "/register") {
        next(); //如果跳转的路径是‘/login’ 或 ‘/regist’ 就正常直接跳转
    } else { // 判断token是否过期 
        isLogin ? next() : next("/login");
    }
})

export default router;
